import {request} from '@/api/service'
import {GetChildren,GetParent,GetObj} from './api'

export const crudOptions = (vm) => {
  return {
    rowHandle: {
      view: {
        thin: true,
        text: null,
      },
      edit: {
        thin: true,
        text: null,
      },
      remove: {
        thin: true,
        text: null,
      },
      custom: [
        {
          thin: true,
          text: null,
          type: 'primary',
          size: 'small',
          emit: 'add-sub-resource',
          icon: 'el-icon-plus',
        }
      ],
      width: 180,
      fixed: 'right'
    },
    options: {
      treeProps: {
        children: 'children', hasChildren: 'hasChildren'
      },
      height: '100%', // 表格高度100%, 使用toolbar必须设置
      highlightCurrentRow: true,
      rowKey: 'id',
      lazy: true,
      load(tree, treeNode, resolve){
        vm.loadTree(tree.id, treeNode, resolve)
      }
    },
    columns: [
      {
        title: '名称',
        key: 'name',
      },
      {
        title: '代码',
        key: 'code',
      },
      {
        title: '排序',
        key: 'sort',
      },
      {
        title: '父节点',
        key: 'pid',
        sortable: true,
        width: 100,
        disabled: true,
        type: 'tree-selector',
        dict: {
          // url: '/category/list',
          isTree: true,
          value: 'id',
          label: 'name',
          // getData: (url, dict) => { // 配置此参数会覆盖全局的getRemoteDictFunc
          //   return request({url: url}).then(ret => {
          //     // alert(1)
          //     // return ret.data
          //     return [{id: 0, name: '根节点', children: ret.data}]
          //   })
          // },
          getNodes(values) { // 配置行展示远程获取nodes
            return new Promise(resolve => {
              let id = values[0];
              if(!id||id === 0) {
                resolve([{id: 0, name: '根节点', children: []}])
              }else {
                GetObj(id).then(res => {
                  resolve([res.data])
                })
              }
            })
          }
        },
        form: {
          component: {
            props: {
              multiple: false,
              elProps: {
                lazy: true,
                load (node, resolve) { // 懒加载
                  let editForm = vm.getEditForm()
                  let exclude = editForm.id;
                  if(node.level === 0){
                    //新增
                    GetChildren({id: 0, exclude}).then(res=>{
                      resolve([{id:0,name: '根节点',children:res.data}])
                    })
                  }else {
                    GetChildren({id: node.data.id, exclude}).then(res=>{
                      resolve(res.data)
                    })
                  }
                  // console.log(node)
                  // if(node.hasChildren){
                  //   GetChildren({id: node.id}).then(res=>{
                  //     resolve(res.data)
                  //   })
                  // }
                }
              },
              dict: {cache: false}
            }
          }
        }
      }
    ]
  }
}
